import json
import re
from Logger import logger

def parse_list(text):
    """
    用 re 从 text 中解析出 [] 标识的 list
    """
    matches = re.findall(r'\[(.*?)\]', text)
    if len(matches) == 0:
        logger.error("No list found")
        return None

    ret = json.loads(f"[{matches[0]}]")
    return ret


def parse_json(text):
    """从response中解析出json"""
    matches = re.findall(r'\{(.*?)\}', text)
    if len(matches) == 0:
        logger.error("No json found")
        return None

    ret = json.loads("{" + matches[0] + "}")
    return ret


def parse_str(text):
    return text


def get_parse_fn(type_name):
    table = {
        "list": parse_list,
        "json": parse_json,
        "str": parse_str
    }
    if type_name in table:
        return table[type_name]
    return parse_str
